<template>
  <el-container class="main-container">
    <!--头部区域-->
    <el-header class="el-header">
      <div>
        <img src="../assets/logo.png" alt="像"/>
        <span>后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!--页面主体-->
    <el-container>
      <!--侧边栏-->
      <el-aside :width="isCollapse?'64px':'200px'">
        <!--展开 和 折叠-->
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu
          :default-active="routerUrl"
          router
          :collapse="isCollapse"
          :collapse-transition="false"
          unique-opened
          background-color="#3F4344"
          text-color="#fff"
          active-text-color="#409eff">
          <!--一级菜单-->
          <el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
            <!--一级菜单的模板区域-->
            <template slot="title">
              <i :class="item.icon"></i>
              <span>{{ item.menuName }}</span>
            </template>
            <!--二级菜单-->
            <el-menu-item :index="subItem.routerUrl" @click="saveNavState(subItem.routerUrl)" v-for="subItem in item.children" :key="subItem.id">
              <template slot="title">
                <i :class="subItem.icon"></i>
                <span>{{ subItem.menuName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!--右侧内容主体-->
      <el-main>
        <!--路由占位符-->
        <router-view/>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      // 左侧菜单数据
      menuList: [],
      // 是否折叠
      isCollapse: false,
      // 保存激活的链接
      routerUrl: ''
    }
  },
  created () {
    this.getMenuList()
    this.routerUrl = window.sessionStorage.getItem('routerUrl')
  },
  methods: {
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList () {
      const { data: res } = await this.$http.get('/vueShop/menus')
      if (!res.status) return this.$message.error(res.msg)
      this.menuList = res.data
    },
    // 菜单的折叠与展开
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    // 点击跳转后 保存 菜单项 高亮 状态
    saveNavState (routerUrl) {
      // 因为会跨vue页面 所以存储位置不同
      window.sessionStorage.setItem('routerUrl', routerUrl)
      this.routerUrl = routerUrl
    }
  }
}
</script>

<style lang="less" scoped>
  .main-container{
    height: 100%;
  }
  .el-header{
    background-color: #3F4344;
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: white;
    font-size: 20px;
    > div {
      display: flex;
      align-items: center;
      img {
        width: 40px;
        height: 40px;
        padding: 5px;
        border-radius: 50%;
        background-color: #eee;
      }
      span {
        margin-left: 15px;
      }
    }
  }
  .el-aside {
    background-color: #3F4344;
    .toggle-button{
      background-color: #6B6E6F;
      font-size: 10px;
      line-height: 24px;
      text-align: center;
      user-select: none;
      letter-spacing: 0.2em;
      color: white;
      cursor: pointer;
    }
    .el-menu {
      border-right: none;
    }
  }
  .el-main{
    background-color: #FAFAFA;
  }
</style>
